﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace VideoFlowStatistics
{
    public partial class FormLines : Form
    {
        public FormLines()
        {
            InitializeComponent();
        }

        private int width = Properties.Settings.Default.GridWidth;//像素宽
        private int height = Properties.Settings.Default.GridHeight;

        System.Collections.Specialized.StringCollection Line1 = Properties.Settings.Default.Line1;
        System.Collections.Specialized.StringCollection Line2 = Properties.Settings.Default.Line2;

        int k = 1;//判断当前线是第几条
        Graphics gra = null;//画图区域
        Pen pen = null;//画笔颜色

        List<int> x = new List<int>();//x坐标集合
        List<int> y = new List<int>();//y坐标集合

        SolidBrush b = new SolidBrush(Color.Red);

        private int xpx, ypx;//画线梅格的长和宽

        private void FormLines_Load(object sender, EventArgs e)
        {
            if (pen == null)
            {
                pen = new Pen(Color.Red, 1);
            }
            if (gra == null)
            {
                gra = pcBox.CreateGraphics();
            }
            if (Line1 == null || Line2 == null)
            {
                Line1 = new System.Collections.Specialized.StringCollection();
                Line2 = new System.Collections.Specialized.StringCollection();
            }
            xpx = pcBox.Width / width;
            ypx = pcBox.Height / height;

            txtMinArea.Text = Properties.Settings.Default.MinArea.ToString();//最小面积
            txtOneArea.Text = Properties.Settings.Default.OneArea.ToString();//面积误差

            if (Properties.Settings.Default.VideoType.ToString() == "1") rbOne.Checked = true;
            else if (Properties.Settings.Default.VideoType.ToString() == "2") rbMany.Checked = true;

            txtwTime.Text = Properties.Settings.Default.WTime.ToString();

            this.txtNetOne.Text = Properties.Settings.Default.NetOne;
            this.txtNetTwo.Text = Properties.Settings.Default.NetTwo;
            //FillRect();
        }

        /// <summary>
        /// 初始化画线
        /// </summary>
        private void FillRect()
        {
            Bitmap bmp = new Bitmap(pcBox.Width, pcBox.Height);
            Graphics draw = Graphics.FromImage(bmp);

            foreach (var item in Line1)
            {
                string[] OneLine = item.Split(';');

                draw.FillRectangle(b, PxtoM(int.Parse(OneLine[0]), pcBox.Width, "w"), PxtoM(int.Parse(OneLine[1]), pcBox.Height, "h"), xpx, ypx);
            }

            foreach (var item in Line2)
            {
                string[] TwoLine = item.Split(';');

                draw.FillRectangle(b, PxtoM(int.Parse(TwoLine[0]), pcBox.Width, "w"), PxtoM(int.Parse(TwoLine[1]), pcBox.Height, "h"), xpx, ypx);
            }
            pcBox.Image = bmp;
        }

        //写入数据
        private void btnOK_Click(object sender, EventArgs e)
        {
            Properties.Settings.Default.Line1 = Line1;
            Properties.Settings.Default.Line2 = Line2;

            Properties.Settings.Default.MinArea = int.Parse(txtMinArea.Text);//最小面积
            Properties.Settings.Default.OneArea = int.Parse(txtOneArea.Text);//一个人的标准面积

            if (rbOne.Checked) Properties.Settings.Default.VideoType = "1";
            else if (rbMany.Checked) Properties.Settings.Default.VideoType = "2";

            try
            {
                Properties.Settings.Default.WTime = double.Parse(txtwTime.Text);
            }
            catch
            {
                MessageBox.Show("间隔时间必须为数字！");
                return;
            }

            Properties.Settings.Default.NetOne = this.txtNetOne.Text;
            Properties.Settings.Default.NetTwo = this.txtNetTwo.Text;

            Properties.Settings.Default.Save();
            //Properties.Settings.Default.Reload();
            this.DialogResult = DialogResult.OK;
        }

        //清除画板
        private void btnClear_Click(object sender, EventArgs e)
        {
            gra.Clear(System.Drawing.Color.White);
            x.Clear();
            y.Clear();
            k = 1;
        }

        //鼠标点下
        private void pcBox_MouseDown(object sender, MouseEventArgs e)
        {
            //判断左键点击，并且判断在边界内
            if (e.Button == MouseButtons.Left && MtoPX(e.X, pcBox.Width, "w") < MtoPX(pcBox.Width, pcBox.Width, "w") && MtoPX(e.Y, pcBox.Height, "h") < MtoPX(pcBox.Height, pcBox.Height, "h"))
            {
                PointAdd(e);
            }
        }

        //鼠标移动
        private void pcBox_MouseMove(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left && MtoPX(e.X, pcBox.Width, "w") < MtoPX(pcBox.Width, pcBox.Width, "w") && MtoPX(e.Y, pcBox.Height, "h") < MtoPX(pcBox.Height, pcBox.Height, "h"))
            {
                PointAdd(e);
                if (x.Count > 2 || y.Count > 2)
                {
                    //gra.DrawLine(pen, x[x.Count - 2], y[y.Count - 2], x[x.Count - 1], y[y.Count - 1]);
                }
            }
        }

        /// <summary>
        /// 将当前鼠标移动点的坐标添加到链表中
        /// </summary>
        /// <param name="e">当前鼠标点击处坐标数据</param>
        private void PointAdd(MouseEventArgs e)
        {
            gra.FillRectangle(b, e.X, e.Y, xpx, ypx);
            int tmpx = MtoPX(e.X, pcBox.Width, "w");
            int tmpy = MtoPX(e.Y, pcBox.Height, "h");
            x.Add(tmpx);
            y.Add(tmpy);

            label2.Text = e.X + "-" + e.Y + "\r\n(" + tmpx + "-" + tmpy + ")";
        }

        //鼠标左键抬起
        private void pcBox_MouseUp(object sender, MouseEventArgs e)
        {
            string point = "{0};{1}";
            string tmpstring = string.Empty;
            //画第一根线后保存数据
            if (k == 1)
            {
                Line1.Clear();
                for (int i = 0; i < x.Count; i++)
                {
                    // Line1.Add(string.Format(point, MtoPX(x[i], pcBox.Width), MtoPX(y[i], pcBox.Height)));
                    if (x[i] < width && y[i] < height)
                    {
                        tmpstring = string.Format(point, x[i], y[i]);
                        if (!Line1.Contains(tmpstring))
                            Line1.Add(tmpstring);
                    }
                }
                k++;
            }
            //画第二根线后保存数据
            else if (k == 2)
            {
                Line2.Clear();
                for (int i = 0; i < x.Count; i++)
                {
                    //Line2.Add(string.Format(point, MtoPX(x[i], pcBox.Width), MtoPX(y[i], pcBox.Height)));
                    if (x[i] < width && y[i] < height)
                    {
                        tmpstring = string.Format(point, x[i], y[i]);
                        if (!Line2.Contains(tmpstring))
                            Line2.Add(tmpstring);
                    }
                }
                //k++;
            }
           
            x.Clear();
            y.Clear();
        }

        /// <summary>
        /// 实际坐标和像素的转换
        /// </summary>
        /// <param name="point">当前坐标点的值（x或y）</param>
        /// <param name="M">实际见面中高或宽</param>
        /// <returns></returns>
        private int MtoPX(int point, int M, string type)
        {
            int k = 0;
            if (type == "w")//算横坐标
                k = (width * point) / M;
            else
                k = (height * point) / M;
            return k;

        }

        /// <summary>
        /// 实际坐标和像素的转换
        /// </summary>
        /// <param name="point">当前坐标的像素点坐标</param>
        /// <param name="M">实际见面中高或宽</param>
        /// <returns>当前坐标点的值（x或y）</returns>
        private int PxtoM(int K, int M, string type)
        {
            int point = 0;
            if (type == "w")
                point = (K * M) / width;
            else
                point = (K * M) / height;//算纵坐标
            return point;

        }

        private void FormLines_FormClosing(object sender, FormClosingEventArgs e)
        {
            Properties.Settings.Default.Reload();
            this.Dispose();
        }
    }
}
